﻿@model DHQGHN.PM0.Models.Admin.GroupInRoleModel
@{
    var lstGroup = ViewBag.lstGroup;
    int columns = 2;
    int rows = (int)Math.Round((double)lstGroup.Items.Count / (double)columns + 0.5);
    int index = 0;
}

@using (
                            Html.BeginForm(
                                actionName: "actionCreateOrUpdateGroupInRole", controllerName: "GroupInRole", routeValues: new { }, method: FormMethod.Post, htmlAttributes: new { @id = "group-in-role-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }
                                )
                            )
{
    <div class="modal-body min-width">
        <div class="form-group">

            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.GroupInRoleID)
			<div>
                <label style = "width: 120px; font-size: 12px;" >Tên ứng dụng</label><span class="field-validation-error"> * </span>
                @Html.DropDownList("AppID", new SelectList(ViewBag.lstApp, "value", "text", 0), new { @id = "input-code-app", @name = "code", @class = "SelectBoxContent", @disabled = "disabled", @style = "width: 70% !important", @placeholder = "" })               
            </div>
            <div>
                @Html.LabelFor(m => m.RoleID, new { @class = "labels", style = "width:120px;" })<span class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.RoleID, new SelectList(ViewBag.lstRole, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 70% !important", @disabled = "disabled" })
                <br /> @Html.ValidationMessageFor(m => m.RoleID, null, new { @class = "Validation_Error", @style = "margin-left:140px" })
            </div>
            <div>
                @Html.LabelFor(m => m.GroupID, new { @class = "labels", style = "width:120px;" })
                <div style="max-height: 200px; overflow: auto; resize: none; width:70%; text-align:right; margin-left:130px">
                    @if (!(lstGroup.Items.Count == 0 || lstGroup == null))
                    {
                        <table class="tbl-list" style="text-align: left; width: 100%;">
                            @for (int i = 0; i < rows; i++)
                            {
                                <tr>
                                    @for (int j = 0; j < columns; j++)
                                    {
                                        index = columns * i + j;
                                        if (index >= lstGroup.Items.Count) { break; }
                                        <td style="width:50%" class="td-onclick">
                                            <div id="checkboxGroupChecked">
                                                <input type="checkbox" name="@lstGroup.Items[index].Value" />&nbsp;&nbsp;&nbsp;@lstGroup.Items[index].Text
                                            </div>
                                        </td>

                                    }
                                </tr>
                            }
                        </table>
                    }
                    else if (ViewBag.AppID > 0)
                    {
                        <span class="field-validation-error" style="margin-left:120px"> Ứng dụng không có hoặc đã thêm hết chức năng </span>
                    }
                </div>
            </div>
            <div>
                <br />
                @Html.LabelFor(m => m.Descriptions, new { @class = "labels", @for = "descriptions", style = "width:120px;" }) <span> &nbsp; </span>
                @Html.TextAreaFor(m => m.Descriptions, new { @class = "TextBoxContent", style = "width:70%; height:150px; vertical-align:top; resize:none", @placeholder = "", @cols = 80, @rows = 10 })
                <br /> @Html.ValidationMessageFor(m => m.Descriptions, null, new { @class = "Validation_Error", @style = "margin-left:140px" })
            </div>
        </div>
    </div>
}

<div id="overlay" class="web_dialog_overlay"></div>
@Scripts.Render("~/bundles/jqueryval")
<script>
        @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script>
    var $groupInRoleForm = $("#group-in-role-form");
    function save() {
        var result = false;
        var listID = getListgroupInRoleID();

        if ($groupInRoleForm.valid()) {
            $("#RoleID").prop("disabled", false);
            $.ajax({
                contentType: "application/json",
                type: "POST",
                url: $groupInRoleForm.attr('action'),
                data: JSON.stringify({ RoleID: $('#RoleID').val().trim(), listID: listID, Descriptions: $('#Descriptions').val().trim() }),
                dataType: 'json',//chú ý là form đặc biệt thì phải điền trường này
                async: false,
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                success: function (response) {
                    if (response != null && response.status == 200) {
                        window.notice(response.result, window.notice_success);
                        OnCriteria(crrPageIndex);
                        result = true;
                    } else {
                        window.notice(response.result, window.notice_error);
                        $("#RoleID").prop("disabled", true);
                    }
                }
            });
        }
        return result;
    }

    $('.td-onclick').click(function () {
        $(this).find(':checkbox').click();
    });

    $(":checkbox").click(function (e) {
        e.stopPropagation();
    });

    function getListgroupInRoleID() {
        var values = [];
        $('#checkboxGroupChecked input:checked').each(function () {
            values.push(this.name);
        });
        return values;
    }
</script>
